import UtilFuncs from '../../common/tools/utilfuncs.js'
var utilfuncs = UtilFuncs.UtilFuncs;

import GlobalConst from '../../common/globalConst.js'
var global = GlobalConst.GlobalConst;

var AbsorbCirclePos = {}

AbsorbCirclePos.calculate = function (mousePt, destPos) {
    var x = destPos.x;
    var y = destPos.y;
    var absobPt = this.calPosOnCircle(mousePt,destPos);
    return absobPt;
}

AbsorbCirclePos.calPosOnCircle = function(mousePt,destPos){
    let x = destPos.x;
    let y = destPos.y;
    var cntPt = {x,y};
    var radius = destPos.radius;
    //y = kx+b;
    //x' = r*cos(a);
    //y' = r*sin(a);

      x = destPos.x;
      y = destPos.y;
    var absobPt = {x,y};
    var dx = mousePt.x - destPos.x;
    var dy = mousePt.y - destPos.y;
    if (Math.abs(dx) < global.ZERO_8E){
        absobPt.x = cntPt.x;
        absobPt.y = cntPt.y + radius;
        if (mousePt.y < cntPt.y){
            absobPt.y = cntPt.y - radius;
        }
    }
    else{
        //y - y0 = k(x - x0)
        var k = dy/dx;
        var b = cntPt.y - k*cntPt.x;
        var lineDirAngle = Math.atan(k);
          x = radius*Math.cos(lineDirAngle) + cntPt.x;
          y = radius*Math.sin(lineDirAngle) + cntPt.y;
        var pt1 = {x,y};
        var dis1 = utilfuncs.distancePoint(mousePt,pt1,1);

        var revertAngle = Math.PI+lineDirAngle;
          x = radius*Math.cos(revertAngle) + cntPt.x;
          y = radius*Math.sin(revertAngle) + cntPt.y;
        var pt2 = {x,y};
        var dis2 = utilfuncs.distancePoint(mousePt,pt2,1);
        absobPt.x = pt1.x;
        absobPt.y = pt1.y;
        if (dis1 > dis2){
            absobPt.x = pt2.x;
            absobPt.y = pt2.y;
        }
    }
    return absobPt;
}

export default{AbsorbCirclePos}